import * as PIXI from 'pixi.js';
import * as UI from '@pixi/ui'
import { DataBus } from '../utils/DataBus';
import { AppComUtils } from '../../uitls/com';
import { $t } from '../../plugins/i18n';
export class Dialog {
    public view: PIXI.Container
    protected scrollBox: UI.ScrollBox
    public title: string
    protected onReady: () => void = () => {}
    constructor(title = 'node') {
        this.title = title
        this.view = new PIXI.Container()
        this.scrollBox = new UI.ScrollBox({
            background: 0x4277b2,
            width: 400,
            height: 300,
            padding: 5,
            elementsMargin: 5,
            radius: 5,
        });
        this.scrollBox.y = 30
        this.view.x = DataBus.app.screen.width / 2 - 200
        this.view.y = DataBus.app.screen.height / 2 - 150
        const bg = new PIXI.Graphics();
        bg.roundRect(0, 0, 400, 330, 5);
        bg.fill({
            color:0x153152
        });
        bg.stroke({
            color:0x3982d4
        })
        this.view.addChild(bg);
        this.view.addChild(this.scrollBox);
        this.view.visible = false
        this.init()
    }
    private async init(){
        const path = AppComUtils.getBasePath()
        const text = new PIXI.Text({
            text: $t(this.title),
            style: {
                fontSize: 18,
                fill: 0xffffff
            }
        })
        text.y = 6
        text.x = 10
        this.view.addChild(text)
        await PIXI.Assets.load(path + 'ui/close.svg')
        const closeIco = PIXI.Texture.from(path + 'ui/close.svg')
        const closeBtn = new UI.Button(new PIXI.Sprite(closeIco))
        closeBtn.view.width = 20
        closeBtn.view.height = 20
        closeBtn.view.x = 375
        closeBtn.view.y = 5
        closeBtn.view.alpha = 0.8
        closeBtn.onPress.connect(()=>{
            this.hide()
        })
        closeBtn.onHover.connect(()=>{
            closeBtn.view.alpha = 1
        })
        closeBtn.onOut.connect(()=>{
            closeBtn.view.alpha = 0.8
        })
        this.view.addChild(closeBtn.view)

        this.onReady()
    }

    show(){
        DataBus.nodesBox.alpha = 0.3
        DataBus.nodesBox.interactiveChildren = false
        this.view.x = DataBus.app.screen.width / 2 - 200
        this.view.y = DataBus.app.screen.height / 2 - 150
        this.view.visible = true
    }
    hide(){
        DataBus.nodesBox.alpha = 1
        this.view.visible = false
        DataBus.nodesBox.interactiveChildren = true
    }
}